from http.server import HTTPServer, BaseHTTPRequestHandler
import urllib.request


def get_ip():
    """
    获取本机公网IP地址
    :return:
    """
    my_ip = urllib.request.urlopen('http://ip.42.pl/raw').read()
    return my_ip


class Request(BaseHTTPRequestHandler):
    def do_GET(self):
        print(self.path)
        self.send_response(200)
        # 标识传递数据类型
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        ip = get_ip()
        self.wfile.write(ip)
        # 下面的形式可以用来传html文件
        # with open('D:\\Python网络编程基础\\Python代码\\http.html','rb') as t:
        #     print('输出了')
        #     self.wfile.write(t.read())


def run():
    host = '0.0.0.0'
    port = 8080
    server = HTTPServer((host, port), Request)
    server.serve_forever()


if __name__ == '__main__':
    # print(Request.path)
    run()
